<template>
    <div>
        <div class="outmain ba_f">
            <div class="shuju_title mar_b20">
                <div class="shuju_title_text">
                    <span>订单提醒</span>
                </div>
            </div>
            <el-form ref="form" :model="form" label-width="180px" :rules="rules">
                <el-form-item label="短信开关提醒" prop="open">
                    <el-radio-group v-model="form.open">
                        <el-radio label="1">开启</el-radio>
                        <el-radio label="2">关闭</el-radio>
                    </el-radio-group>
                    <div class="yb_margin lh16">仅支持阿里云短信通道</div>
                </el-form-item>
                <div v-if="form.open=='1'">
                    <el-form-item label="Access_Key_ID" prop="Access_Key_ID">
                        <el-input v-model="form.access_key_id" placeholder="请输入Access_Key_ID"></el-input>
                        <div class="yb_margin lh16">请输入短信Access_Key_ID</div>
                    </el-form-item>
                    <el-form-item label="Access_Key_Secret" prop="Access_Key_Secret">
                        <el-input v-model="form.access_key_secret" placeholder="请输入access_key_secret"></el-input>
                        <div class="yb_margin lh16">请输入短信Access_Key_Secret</div>
                    </el-form-item>
                    <el-form-item label="模板签名" prop="sign">
                        <el-input v-model="form.sign" placeholder="请输入短信模板签名"></el-input>
                        <div class="yb_margin lh16">请输入短信签名名称</div>
                    </el-form-item>
                    <el-form-item label="接收测试手机号" prop="tel">
                        <el-input v-model="form.tel" placeholder="请输入接收测试手机号" type="number"></el-input>
                    </el-form-item>
                    <div class="shuju_title mar_b20">
                        <div class="shuju_title_text">
                            <span>模板ID设置</span>
                        </div>
                    </div>
                    <el-form-item label="用户注册验证码设置">
                        <el-input v-model="form.registerCode" placeholder="请输入短信模板CODE"></el-input>
                        <el-button class="mar_l20" @click="testTel('1')">测试发送</el-button>
                        <div class="yb_margin lh16">例如：模板内容：您的验证码为89757，请勿告知他人</div>
                    </el-form-item>
                    <el-form-item label="订单支付提醒设置">
                        <el-input v-model="form.payCode" placeholder="请输入短信模板CODD"></el-input>
                        <el-button class="mar_l20" @click="testTel('2')">测试发送</el-button>
                        <div class="yb_margin lh16">例如：模板内容：您的店铺有新的订单，请尽快处理</div>
                    </el-form-item>
                    <el-form-item label="订单退款提醒设置">
                        <el-input v-model="form.refundCode" placeholder="请输入短信模板CODD"></el-input>
                        <el-button class="mar_l20" @click="testTel('3')">测试发送</el-button>
                        <div class="yb_margin lh16">例如：模板内容：您有一条新的退款订单，请登录后台尽快处理</div>
                    </el-form-item>
                    <el-form-item label="积分商城下单提醒设置">
                      <el-input v-model="form.integralCode" placeholder="请输入短信模板CODD"></el-input>
                      <el-button class="mar_l20" @click="testTel('4')">测试发送</el-button>
                      <div class="yb_margin lh16">例如：模板内容：您的积分商城有新的订单，请尽快处理</div>
                    </el-form-item>
                </div>
            </el-form>
        </div>
        <div class="mar_t20 ba_f pad_20 t_c">
            <el-button type="primary" @click="submitForm">保存</el-button>
        </div>
    </div>
</template>
<script>
    import {getConfig, postConfig, testTel} from "@/api/setup";

    export default {
        created() {
            this.bus.$emit('loading', true);
            this.init();
        },
        data() {
            return {
                form: {
                    open: '1',
                    access_key_id: '',
                    access_key_secret: '',
                    sign: '',
                    tel: '',
                    registerCode: '',
                    payCode: '',
                    refundCode: '',
                    integralCode: '',
                },
                rules: {
                    name: [
                        {required: true, message: '请选择短信开关提醒', trigger: 'change'}
                    ],
                    access_key_id: [
                        {required: true, message: '请输入Access_Key_ID', trigger: 'blur'},
                    ],
                    access_key_secret: [
                        {required: true, message: '请输入Access_Key_ID', trigger: 'blur'}
                    ],
                    sign: [
                        {required: true, message: '请输入模板签名', trigger: 'blur'}
                    ],
                    tel: [
                        {required: true, message: '请输入接收测试手机号', trigger: 'blur'}
                    ]
                },
            }
        },
        methods: {
            async init() {
                const {data} = await getConfig({ident: 'sms'});
                this.form2 = this.form
                this.form = Object.assign(this.form2, data);
                this.bus.$emit('loading', false)
            },
            async testTel(type) {
                const {msg} = await testTel({type});
                this.$baseMessage(msg, "success");
                this.init();
            },
            submitForm() {
                this.$refs["form"].validate(async (valid) => {
                    if (valid) {
                        this.form.ident = 'sms'
                        const {msg} = await postConfig(this.form);
                        this.$baseMessage(msg, "success");
                        this.init();
                    } else {
                        return false;
                    }
                });
            },
        }
    }
</script>
